<template>
    <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible"
      :close-on-click-modal="false" draggable>
      <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
       <el-row :gutter="24">
    <el-col :span="12" class="mb20">
      <el-form-item label="居民主键" prop="customerId">
        <el-input v-model="form.customerId" placeholder="请输入居民主键"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="居民姓名" prop="costomerName">
        <el-input v-model="form.costomerName" placeholder="请输入居民姓名"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="药品名称" prop="drug">
        <el-input v-model="form.drug" placeholder="请输入药品名称"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="操作人" prop="operateBy">
        <el-input v-model="form.operateBy" placeholder="请输入操作人"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="操作时间" prop="operateTime">
        <el-input v-model="form.operateTime" placeholder="请输入操作时间"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="操作标识 0-新增 1-修改 2-删除" prop="operateState">
        <el-input v-model="form.operateState" placeholder="请输入操作标识 0-新增 1-修改 2-删除"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="用药方式" prop="idea">
        <el-input v-model="form.idea" placeholder="请输入用药方式"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="用药频率" prop="frequency">
        <el-input v-model="form.frequency" placeholder="请输入用药频率"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="计量单位" prop="unit">
        <el-input v-model="form.unit" placeholder="请输入计量单位"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="服药计量" prop="unitMedicine">
        <el-input v-model="form.unitMedicine" placeholder="请输入服药计量"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="开始时间" prop="startTime">
        <el-input v-model="form.startTime" placeholder="请输入开始时间"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="停药时间" prop="endTime">
        <el-input v-model="form.endTime" placeholder="请输入停药时间"/>
      </el-form-item>
      </el-col>

			</el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<script setup lang="ts" name="BloodxCustomerDrugLogDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/blood/bloodxCustomerDrugLog'
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典

// 提交表单数据
const form = reactive({
		id:'',
	  customerId: '',
	  costomerName: '',
	  drug: '',
	  operateBy: '',
	  operateTime: '',
	  operateState: '',
	  idea: '',
	  frequency: '',
	  unit: '',
	  unitMedicine: '',
	  startTime: '',
	  endTime: '',
});

// 定义校验规则
const dataRules = ref({
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

  // 获取bloodxCustomerDrugLog信息
  if (id) {
    form.id = id
    getbloodxCustomerDrugLogData(id)
  }
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
    loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getbloodxCustomerDrugLogData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>